package com.example.yichuangyun.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MapUtil {

    private static final String PN_GAODE_MAP = "com.autonavi.minimap";// 高德地图包名
    private static final String PN_BAIDU_MAP = "com.baidu.BaiduMap"; // 百度地图包名

    /**
     * 检查地图应用是否安装
     */
    public static boolean isGdMapInstalled(Context context) {
        return isAppInstalled(context, PN_GAODE_MAP);
    }

    public static boolean isBaiduMapInstalled(Context context) {
        return isAppInstalled(context, PN_BAIDU_MAP);
    }

    /**
     * 判断是否安装某一软件
     */
    public static boolean isAppInstalled(Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> pInfo = packageManager.getInstalledPackages(0);
        //存储所有已安装程序的包名
        List<String> pName = new ArrayList<>();
        //从info中将报名字逐一取出
        if (pInfo != null) {
            for (int i = 0; i < pInfo.size(); i++) {
                String pn = pInfo.get(i).packageName;
                pName.add(pn);
            }
        }
        return pName.contains(packageName);
    }


    /**
     * 跳转百度地图
     */
    public static void goToBaiduMap(Context context, String address, double startLatitude, double startLongitude, double endLatitude, double endLongitude) {
        if (!isAppInstalled(context, PN_BAIDU_MAP)) {
            ToastHelper.show("请先安装百度地图客户端");
            return;
        }
        Log.e("@@@@@@@===", startLatitude + "--" + startLongitude + "--" + endLatitude + "--" + endLongitude);
        String uri = "baidumap://map/direction?origin=name:我的位置|latlng"
                + startLatitude + "," + startLongitude
                + "&destination=name:" + address
                + "|latlng" + endLatitude + "," + endLongitude
                + ",&mode=driving&src=andr.baidu.openAPIdemo";
        Intent intent = new Intent();
        intent.setData(Uri.parse(uri));
        context.startActivity(intent);
    }


    /**
     * 跳转高德地图
     */
    public static void goToGaoDeMap(Context context, String address, double startLatitude, double startLongitude, double endLatitude, double endLongitude) {
        if (!isAppInstalled(context, PN_GAODE_MAP)) {
            ToastHelper.show("请先安装高德地图客户端");
            return;
        }
        Intent intent = new Intent();
        intent.setData(Uri.parse("amapuri://route/plan/?sid=&slat=" + startLatitude
                + "&slon=" + startLongitude + "&sname=我的位置&did=&dlat=" + endLatitude
                + "&dlon=" + endLongitude + "&dname=" + address + "&dev=0&t=0"));
        context.startActivity(intent);
    }
}
